package com.b2b.util;

import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Random;


/**
 * 文件传输工具类
 * @author lsj
 * @date 2021/7/22
 */
@Component
public class FileUtil {
    Calendar calendar = Calendar.getInstance();// 获取当前日期
    // 获取当天0点时间戳 Created by lsj
    private Long dayTimeInMillis() {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    // 获取年月日文件路径
    public String getFilePath(){
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
//        return File.separator + year + File.separator + month + File.separator + day + File.separator;
        return year + "/"+ month + "/"+ day + "/";
    }

    // 获取文件名
    public String getFileName(String fileName, String extendedName){
        Random random = new Random();
        int randomInt = random.nextInt(100);
        // 新的图片文件名 = 获取时间戳 + 随机数 + 图片扩展名
        return dayTimeInMillis() + randomInt + extendedName;
    }
}
